const Sequelize = require('sequelize')
const Op = Sequelize.Op
const sequelize = require('../db/dbconfig')
const EntrustModel = require('../db/models/entrusts')
const UserinfoModel = require('../db/models/users')
const entrustModel = EntrustModel(sequelize)
const userinfoModel = UserinfoModel(sequelize)

// 增
// 添加项目模块
exports.addEntrust = async (req, res) => {
  try {
    const userId = req.body?.userId;
    const form = req.body?.form;
    // return res.myMsg('test',200)
    // if (info.form.price !== 0) projectinfo.is_open_source = 0;

    let results = await entrustModel.create({
      user_id: userId,
      tags: JSON.stringify(form.tags),
      ent_title: form.title,
      ent_biography: form.briefly,
      ent_content: form.desc,
      ent_path: JSON.stringify(form.files),
      ent_price: form.amount,
      ent_cover: form.fileCoverList,
    })
    return res.myMsg(results, 200)
  } catch (e) {
    if (e.name === 'SequelizeValidationError') return res.myMsg(e)
    return res.myMsg(e, 500)
  }
}

// 查
exports.getEntrustInfoAll = async (req, res) => {
  try{
    let offset = 0
    entrustModel.belongsTo(userinfoModel,{
      // as: 'cate',
      foreignKey: 'user_id',
      targetKey: 'user_id'
    })
    let results = await entrustModel.findAndCountAll({
      include: [
        {
          attributes: ['nickname'],
          model: userinfoModel
        }
      ],
      where: {
        is_delete: 0,
        // status: 1,
      },
      order: [
          ['ent_id','asc']
      ],
      limit: 10,
      offset
    })
    if(results === '') return res.myMsg('查询错误')
    res.type('application/json');
    res.myMsg(results,200)
  } catch (e) {
    res.myMsg(e)
  }
}
// 根据委托id查询委托信息
exports.getEntrustinfoByEntId = async (req, res) => {
  try {
    // 关联外键
    entrustModel.belongsTo(userinfoModel, {
      // as: 'cate',
      foreignKey: 'user_id',
      targetKey: 'user_id'
    })
    const ent_id = req.body.id;
    let results = await entrustModel.findOne({
      include: [
        {
          attributes: ['username'],
          model: userinfoModel,
        }
      ],
      where: {
        is_delete: 0,
        ent_id: ent_id,
      },
    })
    if (results === null || results === '') return res.myMsg('查询失败,不存在该委托', 404)
    console.log('res', results)
    // res.type('application/json');
    // res.jsonp(results);
    return res.myMsg(results, 200)
  } catch (e) {
    res.myMsg(e)
  }
}

// 删
exports.delEntrust = async (req, res) => {
  try {
    let results = await entrustModel.update({
      is_delete: 1
    },{
      where: {
        ent_id: req.body.ent_id
      }
    })
    if(results[0] === 0) return res.myMsg('委托不存在，删除失败', 500)
    return res.myMsg('委托删除成功',200)
  } catch (e) {
    res.myMsg(e)
  }
}


// 根据id改委托状态
exports.updateStatusByEntrustId = async (req, res) => {
  try {
    const ent_id = req.body.ent_id;
    const status = req.body.status;
    let is_exist = await entrustModel.findOne({
      where: {
        is_delete: 0,
        ent_id: ent_id
      }
    })
    if (is_exist === null) return res.myMsg('委托不存在，修改失败', 500)
    let results = await entrustModel.update({
      status: status,
    }, {
      where: {
        ent_id: ent_id,
      }
    })
    if (results[0] === 0) return res.myMsg('请求成功,无改动', 200);
    return res.myMsg('修改成功', 200)
  } catch (e) {
    res.myMsg(e)
  }
}